package creator.ui.graphics.bean;

import creator.ui.graphics.Magicanvas;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;

public abstract class Bean implements BeanSpring {

  public static final String ALL_TYPE = "*";
  private static MediaTracker TRACKER;
  public static int trackerID = 0;
  public ArrayList<Image> BEANLIST;

  public MediaTracker getTracker() {
    return TRACKER;
  }

  private static int getTrackerID() {
    return trackerID++;
  }

  public static void trackerImages(
    ArrayList<Image> images,
    Magicanvas magicanvas
  ) {
    if (TRACKER == null) TRACKER = new MediaTracker(magicanvas);
    try {
      for (Image img : images) {
        TRACKER.addImage(img, getTrackerID());
      }
      TRACKER.waitForAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void trackerImage(Image image, Magicanvas magicanvas) {
    if (TRACKER == null) TRACKER = new MediaTracker(magicanvas);
    try {
      TRACKER.addImage(image, getTrackerID());
      TRACKER.waitForAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public Image getImage(String path) {
    File imageFile = new File(Bean.class.getResource(path).getFile());

    return Toolkit
      .getDefaultToolkit()
      .getImage(
        imageFile
          .getPath()
          .replace("file:\\", "")
          .replace("FlappyBird.jar!\\", "")
      );
  }

  public ArrayList<Image> getGroupOfImages(String[] pathList) {
    ArrayList images = new ArrayList();
    for (String path : pathList) {
      images.add(getImage(path));
    }
    return images;
  }

  public ArrayList<Image> getGroupOfImages(ArrayList<String> pathList) {
    ArrayList images = new ArrayList();
    for (String path : pathList) {
      images.add(getImage(path));
    }
    return images;
  }

  private ArrayList<String> pathFilter(String path, String type) {
    ArrayList paths = new ArrayList();
    File file = new File(path);
    if (file.isDirectory()) {
      File[] listFiles = file.listFiles();
      String fileType = "";
      for (File f : listFiles) {
        fileType = f.getName().substring(f.getName().indexOf(".") + 1);
        if ((fileType.equals(type)) || (type.equals("*"))) {
          fileType = f.getParent().replace("\\", "/") + "/" + f.getName();
          if (f.isDirectory()) fileType = fileType + "/";
          paths.add(fileType);
        }
      }
    } else {
      paths.add(path);
    }
    return paths;
  }

  public ArrayList<String> imagePathFilter(String store, String[] types) {
    ArrayList paths = new ArrayList();
    for (String type : types) {
      if (isImage(type)) {
        paths.addAll(pathFilter(store, type));
      }
    }
    return paths;
  }

  private boolean isImage(String type) {
    return (
      (type.equalsIgnoreCase("png")) ||
      (type.equalsIgnoreCase("gif")) ||
      (type.equalsIgnoreCase("jpg"))
    );
  }

  public ArrayList<Image> getBeanList() {
    if (this.BEANLIST == null) this.BEANLIST = new ArrayList();
    return this.BEANLIST;
  }
}
